package cas;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author fei
 * @date 2023/1/17 16:53
 * @description
 */
public class AtomicStampedDemo {

    public static void main(String[] args) {
        Book javaBook = new Book(1, "javaBook");
        Book mysqlBook = new Book(2, "mysqlBook");
        AtomicStampedReference<Book> stampedReference = new AtomicStampedReference<>(javaBook, 1);
        // getStamp：版本号
        System.out.println(stampedReference.getReference() + "\t" + stampedReference.getStamp());

        // 第三个参数：旧版本号，如果现在的版本号不是这个，就不进行更新
        // 第四个参数：新版本号，更新后，将版本号也更新上去
        boolean b = stampedReference.compareAndSet(javaBook, mysqlBook,
                stampedReference.getStamp(), stampedReference.getStamp());
    }

}

class Book{

    int id;
    String bookName;

    public int getId() {
        return id;
    }

    public String getBookName() {
        return bookName;
    }

    public Book() {
    }

    public Book(int id, String bookName) {
        this.id = id;
        this.bookName = bookName;
    }
}
